package ncku.account.event;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import ncku.account.data.Category;
import ncku.account.data.GlobalData;
import ncku.account.data.SubTransaction;
import ncku.account.data.Transaction;
import ncku.account.gui.categorySel;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/*
 使用者選擇匯出帳戶，會以帳戶名稱開啟Excel 檔案，
 並把帳戶金錢，以及交易資料逐筆輸入進去。
  */
public class Export {
	private static ArrayList<Transaction> transactions;
	private ArrayList<SubTransaction> subTransactions;  //子細項
	private int cellPos;
	private String[] cateGoryList;
	public Export(){
		//建構時把程式現在正在運行的transaction list 傳過來
		transactions = GlobalData.getTranForExport();
		//excel 目前格子位置變數
		cellPos = 2;
		cateGoryList = categorySel.getCategoriesList();
	}
	
	//開始匯出帳戶，以帳戶為名稱開啟Excel 檔案，裡面的表格內容為交易資料
	public String Start(){
		try{
			//開啟指定帳戶名稱的excel 檔案
	        WritableWorkbook workbook = Workbook.createWorkbook(new File(GlobalData.accountName+".xls"));
	        WritableSheet sheet = workbook.createSheet("My Sheet", 0);
	        //創建檔案開頭預設表格
	        Label labelMoneyString = new Label(0, 0, "帳戶餘額");
	        Label labelMoney = new Label(1, 0, Integer.toString(GlobalData.balanceMoney));
	        Label labelInOne = new Label(1, 1, "日期");
	        Label labelInTwo = new Label(2, 1, "類別");
	        Label labelInThree = new Label(3, 1, "描述");
	        Label labelInFour = new Label(4, 1, "金額");
	        Label labelCateGoryNumber = new Label(6, 0,Integer.toString(GlobalData.getCategoryNumber()));   
	        //加入表格
	        sheet.addCell(labelMoney);
	        sheet.addCell(labelMoneyString);
	        sheet.addCell(labelInOne);
		    sheet.addCell(labelInTwo);
		    sheet.addCell(labelInThree);
		    sheet.addCell(labelInFour);
		    sheet.addCell(labelCateGoryNumber);
		    for(int i=1; i <= transactions.size();i++){    	   
		    	//將交易資料依照日期，類別，描述，花費金錢逐筆加入excel 檔   
		       Label labelDate = new Label(1,cellPos,GlobalData.getTransaction(i-1).getDate());
		       Label labelCategory = new Label(2,cellPos, GlobalData.getTransaction(i-1).getCategory());
		       Label labelDescription = new Label(3,cellPos,transactions.get(i-1).getDescription());
		       Label labelStringAmount= new Label(4,cellPos,GlobalData.getTransaction(i-1).getStringAmount()); 
		       //加入表格
		       sheet.addCell(labelDate);
			   sheet.addCell(labelCategory);
			   sheet.addCell(labelDescription);
			   sheet.addCell(labelStringAmount);
			   //取得子細項的陣列
			   subTransactions = transactions.get(i-1).getSubTransactionForExport();
			   if(subTransactions.size() != 0){
				   //#字符號記錄在最前面代表此交易有子項目，方便import 用
				   Label labelSubFlag= new Label(0,cellPos,"#"); 
			       sheet.addCell(labelSubFlag);
				   for(int j = 1; j <= subTransactions.size();j++){
					   //讀子細項的細節以及金錢
					   Label labelSubDescription = new Label(3,cellPos+j,subTransactions.get(j-1).getSubDescription());
					   Label labelSubAmount= new Label(4,cellPos+j,subTransactions.get(j-1).getSubStringAmount()); 
					   sheet.addCell(labelSubDescription);
					   sheet.addCell(labelSubAmount);
				   }
				   //全部子細項加完，表格位置也要加
				   cellPos = cellPos + subTransactions.size() +1;
			   }
			   else
				   cellPos++;
			 
			  
	       }
		    
		    for(int i = 0; i < GlobalData.getCategoryNumber(); i++){
		    	 Label labelCateGory = new Label(6, i+1,cateGoryList[i] );
		    	 sheet.addCell(labelCateGory);
			}
		    
		    //最後寫入，並關閉
	        workbook.write(); 
	        workbook.close();
	        
	    } 
		catch (IOException e1){
	        return "expection";
	    } 
		catch (WriteException e){
	        return "writeError";
	    }
	    catch (ArrayIndexOutOfBoundsException e){
	    	return "ArrayIndexError";
		 }
		return "success";
	}
		
}